package router;

import org.springframework.util.AntPathMatcher;
import bo.RouteObject;
import property.RouteProperty;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class RandomHttpEndpointRouter implements HttpEndpointRouter {
    @Override
    public String route(RouteProperty routeProperty,String requestUrl) {
        AntPathMatcher antPathMatcher=new AntPathMatcher();
        for(RouteObject routeObject:routeProperty.getRoutes())
        {
            if(antPathMatcher.match(routeObject.getUrl(),requestUrl))
            {
                List<String> servers = Arrays.asList(routeObject.getServer().split(","));
                int size = servers.size();
                Random random = new Random(System.currentTimeMillis());
                return servers.get(random.nextInt(size));
            }

        }
        return "";
    }
}